package com.chanjiedata.changbs.module.main;

/**
 * 文件名 : MessageTypeFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 14:59
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.adapter.RankAdapter;
import com.chanjiedata.changbs.base.BaseFragment;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Recharge;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by AndroidIntexh1 on 2018/6/4.
 */

public class RankFragment extends BaseFragment {
    @BindView(R.id.fragment_recycleView)
    RecyclerView mRecycleView;
    Unbinder unbinder;
    TextView tvUnit;
    TextView tvPhone;
    ImageView ivLevel;
    TextView tvRownum;
    TextView tvAmount;

    private int position;

    private List<Recharge.earnings_ranking.List> beans = new ArrayList<>(); // 提现消息
    private RankAdapter mAdapter;


    public static RankFragment NewInstance(int instanceType) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", instanceType);
        RankFragment messageTypeFragment = new RankFragment();
        messageTypeFragment.setArguments(bundle);
        return messageTypeFragment;
    }


    /**
     * 获取布局文件
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycleview;
    }


    /**
     * 初始化操作步骤
     */
    @Override
    protected void init() {
        unbinder = ButterKnife.bind(this, rootView);
        position = getArguments().getInt("position");
    }

    /**
     * 初始化界面
     */
    @Override
    protected void initView() {
        initRecycle();
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        Recharge.earnings_ranking.Builder builder = Recharge.earnings_ranking.newBuilder();

        if (position == 0) {
            builder.setType("trading");
        } else {
            builder.setType("activation");
        }
        NetworkManager.INSTANCE.post(Apis.earnings_ranking, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                List<Recharge.earnings_ranking.List> lists = Recharge.earnings_ranking.parseFrom(response).getArrayList();
                Recharge.earnings_ranking.MyRanking bean = Recharge.earnings_ranking.parseFrom(response).getMyRanking();
                tvAmount.setText(bean.getAmount() + "");
                tvPhone.setText(bean.getLgMemberName());
                if (Integer.valueOf(bean.getRownum()) > 999) {
                    tvRownum.setText("999+");
                } else {
                    tvRownum.setText(bean.getRownum());
                }
                if (position == 0) {
                    tvUnit.setText("元");
                } else if (position == 1) {
                    tvUnit.setText("台");
                }
                mAdapter.addData(lists);
                mAdapter.loadMoreEnd(true);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }


    /**
     * 实例化线性底部RecyclerView
     * *
     */
    private void initRecycle() {
        mRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycleView.setAdapter(mAdapter = new RankAdapter(mContext, beans));
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setEmptyView(R.layout.recycle_empty);
        View headerView = LayoutInflater.from(mContext).inflate(R.layout.item_rank_head, null);
        tvUnit = headerView.findViewById(R.id.tv_unit);
        tvRownum = headerView.findViewById(R.id.tv_rownum);
        tvPhone = headerView.findViewById(R.id.tv_phone);
        tvAmount = headerView.findViewById(R.id.tv_amount);
        mAdapter.addHeaderView(headerView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }
}
